<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>
<script type="text/javascript" src="/ajaxupload.js"></script>
<div style="position:relative;">
<b><u>Зображення до розділу:</u></b><br/>
<?php if ($model->getId()):?>
<div id="ajax_loader" style="display:none;"><img alt=""  src="/images/ajax-loader.gif" /></div>
	<div id="img_upload_button">Загрузити</div>

	<ul id="additional_images" style="padding-top:5px;">
		<?php $images = $model->getAdditionalImages();
                //var_dump ($images); die();
			if( !empty($images) ): ?>
				<?php foreach($images as $image):?>
                                       <?php // var_dump($image->attributes)?>
            <li id='image<?php echo $image->GetId()?>'><img alt=""  src='/uploads/thumbnail_90_90/<?php echo $image->image?>'/>
						<a id='remove_link' onclick='if(confirm("Ви впевнені?")){deleteImage(<?php echo $image->getId()?>)}' href='javascript://' title='Видалити зображення'>Удалить</a>
					</li>
				<?php endforeach;?>
			<?php endif;?>
	</ul>

	<div class="clear"></div>

	<script type="text/javascript">
		$(document).ready(function() {
		new AjaxUpload('img_upload_button', {
			  // Location of the server-side upload script
			  // NOTE: You are not allowed to upload files to another domain
			  action: '/admin/index.php/news/upload',
			  // File upload name
			  name: 'userfile',
			  // Additional data to send
			  data: {
			    object_id : <?php echo $model->getId()?>,
			    object_enity : 'news'
			  },
			  // Submit file after selection
			  autoSubmit: true,
			  // The type of data that you're expecting back from the server.
			  // HTML (text) and XML are detected automatically.
			  // Useful when you are using JSON data as a response, set to "json" in that case.
			  // Also set server response type to text/html, otherwise it will not work in IE6
			  responseType: false,
			  // Fired after the file is selected
			  // Useful when autoSubmit is disabled
			  // You can return false to cancel upload
			  // @param file basename of uploaded file
			  // @param extension of that file
			  onChange: function(file, extension){$('#ajax_loader').show()},
			  // Fired before the file is uploaded
			  // You can return false to cancel upload
			  // @param file basename of uploaded file
			  // @param extension of that file
			  onSubmit: function(file, extension) {},
			  // Fired when file upload is completed
			  // WARNING! DO NOT USE "FALSE" STRING AS A RESPONSE!
			  // @param file basename of uploaded file
			  // @param response server response
			  onComplete: function(file, response) {
				  	$('#ajax_loader').hide();
				  	var obj = eval("("+response+")");
					if (obj.imageid) {
						 var el = "<li id='image"+obj.imageid+"'><img src='/uploads/thumbnail_90_90/" + obj.filename + "'/><br/>" +
								 "<a id='remove_link' onclick='if(confirm(\"Ви впевнені?\")){deleteImage("+obj.imageid+")}' href='javascript://' title='Видалити зображення'>Удалить</a></li>";
						$('#additional_images').append(el);
						alert('Файл ['+file+'] завантажений успішно');
					} else {
						alert(obj.error);
					}
				}
			});
	});
	function deleteImage(image_id)
	{
		$.ajax({
			type: "POST",
			url: "/admin/index.php/news/remove",
			data: "image_id="+image_id,
			beforeSend: function(){ $('#ajax_loader').show(); },
			success: function(msg){
					if(msg=='success') {
						$('#ajax_loader').hide();
						$('#image'+image_id).remove();
					} else {
						alert(msg);
					}
				}
			});
	}
	</script>
	<?php else:?>
		<br/>Ви можете додавати додаткові картинки, але для цього спочатку<br />
                збережіть сторінку.    
	<?php endif;?>
</div>